//
//  ShowNumView.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/20.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "ShowNumView.h"
#import "HisNumCell.h"
@interface ShowNumView ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UIView *whiteView;
    //瀑布流
    UICollectionView *_collectionView;
    UILabel *countLab;
    //数据源
    NSMutableArray *_data;
    //记录id
    NSString *_psc_id;
    NSString *_qishu;
    NSString *_owner_id;
}

@end
@implementation ShowNumView

- (instancetype)initWithArr:(NSMutableArray *)data AndQiShu:(NSString *)qishu AndTitle:(NSString *)title
{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, WYMWITH, WYMHEIGHT);
        _qishu = qishu;
        _data = data;
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        whiteView = [[UIView alloc]initWithFrame:CGRectMake(20, 0, WYMWITH - 40, WYMHEIGHT - 250*app.autoSizeScaleY)];
        whiteView.backgroundColor = [UIColor whiteColor];
        whiteView.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2);
        //分割线
        [whiteView addSubview:[WYPublic getFenGexian:0 :50 :whiteView.frame.size.width :0.5]];
        UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, whiteView.frame.size.width-20, 50)];
        titleLab.numberOfLines = 2;
        
        titleLab.font = [UIFont systemFontOfSize:14];
        titleLab.text = [NSString stringWithFormat:@"(第%@期)%@",qishu,title];
        [whiteView addSubview:titleLab];
        
        countLab = [[UILabel alloc]initWithFrame:CGRectMake(4, CGRectGetMaxY(titleLab.frame)+2, whiteView.frame.size.width - 8, 20)];
        countLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"参与" Middle:[NSString stringWithFormat:@"%ld",(unsigned long)_data.count] RightStr:@"次,夺宝号码" Font:14];
        countLab.font = [UIFont systemFontOfSize:14];
        [whiteView addSubview:countLab];
        //瀑布流
        [self loadCollectionView];
        
        //分割线
        [whiteView addSubview:[WYPublic getFenGexian:0 :whiteView.frame.size.height - 50 :whiteView.frame.size.width :0.5]];
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        backBtn.frame = CGRectMake(0, 0, 200, 50);
        backBtn.center = CGPointMake(whiteView.frame.size.width/2, whiteView.frame.size.height - 25);
        [backBtn setTitle:@"确定" forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(backBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [whiteView addSubview:backBtn];
        [self addSubview:whiteView];
        [_collectionView reloadData];
    }
    return self;
    
}
//确定按钮的监听事件
- (void)backBtnOnClick
{
    [self removeFromSuperview];
}
//用瀑布流显示号码
- (void)loadCollectionView
{
    //初始化瀑布流
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(countLab.frame), whiteView.frame.size.width, whiteView.frame.size.height - 102-countLab.frame.size.height) collectionViewLayout:layout];
    //设置代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //注册cell
    UINib *nib = [UINib nibWithNibName:@"HisNumCell" bundle:nil];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"];
    [whiteView addSubview:_collectionView];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    return CGSizeMake(whiteView.frame.size.width/3.0, 20*myDelegate.autoSizeScaleY);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _data.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HisNumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (_data.count>0) {
        //获取当前数据模型
        NSString *row = _data[indexPath.row];
        //填充cellj
        BOOL isRed = NO;
        if ([self.owner_code isEqualToString:row]){
            isRed = YES;
        }
        [cell fillCellWithTitle:row isRedColor:isRed];
        
    }
    //    cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return cell;
}


@end
